import japa.parser.*;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.Node;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.type.ClassOrInterfaceType;
import japa.parser.ast.visitor.ModifierVisitorAdapter;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class DepthOfInheritance {

	/**
	 * Calculates the depth of inheritance
	 */

	public static void main(String[] args) throws Exception {

		final JFileChooser fc = new JFileChooser();

		fc.showOpenDialog(new JFrame());

		// creates an input stream for the file to be parsed
		File file = fc.getSelectedFile();
		System.out.println(file.getName());

		FileInputStream in = new FileInputStream(file.getAbsolutePath());

		CompilationUnit cu;
		try {
			// parse the file
			cu = JavaParser.parse(in);
		} finally {
			in.close();
		}

		// visit and print the methods names
		NPMVisitor mv = new NPMVisitor();
		mv.visit(cu, null);

	}

	/**
	 * Simple visitor implementation for visiting MethodDeclaration nodes.
	 */
	private static class NPMVisitor extends ModifierVisitorAdapter {
		int count = 0;

		@Override
		public Node visit(ClassOrInterfaceDeclaration n, Object arg) {
			// here you can access the attributes of the method.
			// this method will be called for all methods in this
			// CompilationUnit, including inner class methods

			count = 0;
			getSuper(n.getClass());
			System.out.println("The DIT:" + count);

			java.util.List<ClassOrInterfaceType> extendz = n.getExtends();
			if (extendz != null) {

				for (ClassOrInterfaceType coit : extendz)
					System.out.println("\t" + coit.getName());

				System.out.println("This class extends " + extendz.size());
			}

			return n;

		}

		private Class getSuper(Class branch) {

			if (branch != null) {
				count++;
				System.out.println("\t" + branch.getName());
				return getSuper(branch.getSuperclass());
			}

			else
				return null;

		}

	}

}